package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.AlarmPushDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.dto.SignalLampDto;
import com.hualude.roadsign.model.SignalLampEntity;
import com.hualude.roadsign.repository.AlarmPushRepository;
import com.hualude.roadsign.repository.AlarmRepository;
import com.hualude.roadsign.repository.SignalLampFaultRepository;
import com.hualude.roadsign.repository.SignalLampRepository;
import com.hualude.roadsign.service.SignalLampManageService;
import com.hualude.roadsign.utils.FormatNumber;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2017/8/2.
 */
@Service
public class SignalLampManageServiceImpl implements SignalLampManageService {
    @Resource
    private SignalLampRepository signalLampRepository;
    @Resource
    private AlarmRepository alarmRepository;
    @Resource
    private SignalLampFaultRepository signalLampFaultRepository;
    @Resource
    private AlarmPushRepository alarmPushRepository;

    @Override
    public DTResponseDto getSignalLamp4DT(DTRequestDto dtRequestDto,
                                          String actorIds,
                                          Integer orgId,
                                          Boolean audit,
                                          String startRoad,
                                          String endRoad,
                                          Integer alarmBinding) throws Exception {
        List<SignalLampDto> signalLampDtos = signalLampRepository.getSignalLamp4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, alarmBinding);
        Integer count = signalLampRepository.getSignalLamp4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, alarmBinding);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, signalLampDtos);
        return dtResponseDto;
    }

    @Override
    public List<SignalLampDto> getSignalLamp4APP(
            String actorIds, Integer orgId, Integer mCount,
            Integer mSize, String city, String startRoad, String endRoad, String alarmType) throws Exception {
        return signalLampRepository.getSignalLamp4APP(
                actorIds, orgId, mCount, mSize, city, startRoad, endRoad, alarmType);
    }

    @Override
    public SignalLampDto getSignalLampById(Integer signalLampById) throws Exception {
        return signalLampRepository.getSignalLampById(signalLampById).get(0);
    }

    @Override
    public List<String> getStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return signalLampRepository.getStartRoad(roadName, actorId, orgId);
    }

    @Override
    public List<String> getSectionByStartRoad(String roadName, Integer actorId, Integer orgId) throws Exception {
        return signalLampRepository.getSectionByStartRoad(roadName, actorId, orgId);
    }

    @Override
    @Transactional
    public Integer deleteSignalLamp(String signalLampIds) throws Exception {
        String[] signalLampIdArray = signalLampIds.split(",");
        for (String sinalLampId : signalLampIdArray) {
            signalLampFaultRepository.deleteLampFault(Integer.parseInt(sinalLampId));
            signalLampRepository.deleteSignalLamp(Integer.parseInt(sinalLampId));
        }
        return signalLampIdArray.length;
    }

    @Override
    @Transactional
    public Integer addSignalLamp(SignalLampDto signalLampDto) throws Exception {
        signalLampDto.setSignalLampLongitude(FormatNumber.resize(signalLampDto.getSignalLampLongitude()));
        signalLampDto.setSignalLampLatitude(FormatNumber.resize(signalLampDto.getSignalLampLatitude()));
        Integer devicePathId = signalLampRepository.save(signalLampDto.toBean()).getSignalLampId();
        String deviceId = signalLampDto.getSignalLampADeviceId();
        if (!deviceId.isEmpty()) {
            alarmRepository.updapteAlarmPath(0, devicePathId, deviceId);
        }
        return devicePathId;
    }

    @Override
    @Transactional
    public Integer updateSignalLamp(SignalLampDto signalLampDto) throws Exception {
        return signalLampRepository.updateSignalLamp(
                signalLampDto.getSignalLampId(),
                signalLampDto.getProjectNumber(),
                signalLampDto.getSignalLampRegionId(),
                signalLampDto.getSignalLampLongitude(),
                signalLampDto.getSignalLampLatitude(),
                signalLampDto.getSignalLampProvince(),
                signalLampDto.getSignalLampCity(),
                signalLampDto.getSignalLampArea(),
                signalLampDto.getSignalLampStartRoad(),
                signalLampDto.getSignalLampEndRoad(),
                signalLampDto.getSignalLampType(),
                signalLampDto.getSignalLampBrand(),
                signalLampDto.getSignalLampPhoto(),
                signalLampDto.getSignalLampInstallTime(),
                signalLampDto.getSignalLampModifierId(),
                signalLampDto.getSignalLampPeriod(),
                signalLampDto.getSignalLampServiceLife(),
                signalLampDto.getSignalLampRemark(),
                signalLampDto.getSignalLampInstallOrg(),
                signalLampDto.getSignalLampMainOrgId(),
                0);
    }

    @Override
    public List<String> getCity4SignalLamp(Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return signalLampRepository.getCity4SignalLamp(actorId, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByCity(String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return signalLampRepository.getStartRoadByCity(city, actorId, orgId, mCount, mSize);
    }

    @Override
    public List<SignalLampDto> getSignalLamp4Map4App(Integer orgId,
                                                     String actorIds,
                                                     double neLongitude,
                                                     double neLatitude,
                                                     double swLongitude,
                                                     double swLatitude) throws Exception {
        return signalLampRepository.getSignalLamp4Map4App(orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
    }

    @Override
    public List<String> getSectionByStartRoad4App(String roadName, String city, Integer actorId, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return signalLampRepository.getSectionByStartRoad4App(roadName, city, actorId, orgId, mCount, mSize);
    }

    @Override
    @Transactional
    public Integer auditSignalLamp(String signalLampIds) throws Exception {
        String[] signalLampIdArrary = signalLampIds.split(",");
        for (String signalLampId : signalLampIdArrary) {
            signalLampRepository.auditSignalLamp(Integer.parseInt(signalLampId));
        }
        return signalLampIdArrary.length;
    }

    @Override
    @Transactional
    public Integer bindingDevice(String deviceId, Integer signalLampId) throws Exception {
        SignalLampEntity entity = signalLampRepository.findOne(signalLampId);
        StringBuffer position = new StringBuffer();
        position.append(entity.getSignalLampProvince());
        position.append(entity.getSignalLampCity());
        position.append(entity.getSignalLampArea());
        position.append(" ");
        position.append(entity.getSignalLampStartRoad());
        position.append("-");
        position.append(entity.getSignalLampEndRoad());
        int count1 = signalLampRepository.bindingDevice(deviceId, signalLampId);
        count1 += alarmRepository.bindingDevice(2, signalLampId, position.toString(), deviceId);
        return count1;
    }

    @Override
    @Transactional
    public Integer unbindingDevice(String deviceId, Integer highLeverId) throws Exception {
        int count = signalLampRepository.unbindingDevice(highLeverId);
        count += alarmRepository.unbindingDevice(deviceId);
        return count;
    }

    @Override
    public DTResponseDto showAlarmHistory(DTRequestDto dtRequestDto, String deviceId, Integer deviceType) throws Exception {
        List<AlarmPushDto> dtos = alarmPushRepository.getAlarmHistory4DT(dtRequestDto, deviceId, deviceType);
        int count = alarmPushRepository.getAlarmHistoryCount4DT(dtRequestDto, deviceId, deviceType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public List<SignalLampDto> getDeviceInfo(Integer deviceId) throws Exception {
        return signalLampRepository.getDeviceInfo(deviceId);
    }

}
